package com.itheima.mapper;

import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

@Mapper
//在程序运行时，动态的生成一个该接口的实现类对象，并放到Spring容器中
public interface EmpMapper {

    //当我们调用delete方法时，会自动执行sql语句
    @Delete("delete from mybatis.emp where emp.id =#{id} ")
    int delete(Integer id);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    //useGeneratedKeys :要使用数据库自动生成的主键，并获取出来
    //keyProperty :放到
    @Insert("insert into mybatis.emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)" +
            " values (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")
    void insert(Emp emp);

    //删除@Update注解编写的SQL语句
    //update操作的SQL语句编写在Mapper映射文件中
//    @Update("update emp set username=#{username}, name=#{name}, gender=#{gender}, image=#{image}, " +
//            "job=#{job}, entrydate=#{entrydate}, dept_id=#{deptId}, update_time=#{updateTime} where id=#{id}")
    void update(Emp emp);

    @Select("select * from mybatis.emp where id =#{id}")
    Emp select(Integer id);

    /*@Select("select *\n" +
            "from emp\n" +
            "where name like concat('%',#{name},'%')\n" +
            "and gender =#{gender}\n" +
            "and (entrydate between #{begin} and #{end})\n" +
            "order by update_time desc")*/
    List<Emp> list(String name, Short gender
            , LocalDate begin, LocalDate end);

    //@Delete("delete from emp where id in(4,5,6)")
    void deleteByIds(List<Integer> ids);

}
